//
//  JMTool.m
//  NingBoBank
//
//  Created by dev on 13-9-4.
//
//

#import "JMTool.h"
#include <AddressBook/ABPerson.h>

@implementation JMTool 
#pragma mark-
+(void) callMapShowPathFromCurrentLocationFrom:(CLLocationCoordinate2D)f To:(CLLocationCoordinate2D)desCoordinate andDesName:(NSString *)desName  withAlertView:(UIView*) view;
{
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0f) {
        //ios_version为4.0～5.1时 调用谷歌地图客户端
        
        //生成url字符串
        NSString *currentLocation = [NSString stringWithFormat:@"%f,%f",
                                     f.latitude,
                                     f.longitude];;//[LocalizedCurrentLocation currentLocationStringForCurrentLanguage];
        NSString *desLocation = [NSString stringWithFormat:@"%f,%f",
                                 desCoordinate.latitude,
                                 desCoordinate.longitude];
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
                               currentLocation,desLocation];
        //转换为utf8编码
        urlString =  [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        
        UIApplication *app =[UIApplication sharedApplication];
        NSURL *url = [NSURL URLWithString:urlString];
        
        //验证url是否可用
        if ([app canOpenURL:url]) {
            [app openURL:url];
        }else{
            //手机客户端不支持此功能 或者 目的地有误
            [view showAlertTitle:@"不能打开该地址"
                                andContent:@"目的地位置可能有误"];
        }
        
    } else if([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0f &&
              [[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f ) {
        //ios_version为 >=6.0时 调用苹果地图客户端
        
        //验证MKMapItem的有效性
        Class itemClass = [MKMapItem class];
        if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
            desName = desName!=nil?desName:@"目的地";
            NSDictionary *dicOfAddress = [NSDictionary dictionaryWithObjectsAndKeys:
                                          desName,(NSString *)kABPersonAddressStreetKey, nil];
            MKPlacemark *palcemake = [[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:dicOfAddress];
            MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:palcemake];
            
            NSDictionary *dicOfMode = [NSDictionary dictionaryWithObjectsAndKeys:
                                       MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];
            //打开客户端
            if (![mapItem openInMapsWithLaunchOptions:dicOfMode]) {
                //打开失败
                [view showAlertTitle:@"不能打开该地址"
                                    andContent:@"目的地位置可能有误"];
            }
        }
        
    } else {
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];//调用自带地图（定位）
        //显示目的地坐标。画路线
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc]initWithCoordinate:desCoordinate addressDictionary:nil] autorelease]];
        toLocation.name = desName!=nil?desName:@"目的地";
        [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
                       launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil]
                                      
                                                                 forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
        
        [toLocation release];
    }

}

//拨打电话
+(void)callPhoneNum:(NSString*) aPhoneNum{
    
    //    if([deviceType  isEqualToString:@"iPod touch"]||[deviceType  isEqualToString:@"iPad"]||[deviceType  isEqualToString:@"iPhone Simulator"])
    
    UIDevice *_device = [UIDevice currentDevice];
    NSString *_model = [_device model];
    if ([_model isEqualToString:@"iPhone"] || [_model isEqual:@"iPhone4s"]) {
        NSString* str = [NSString stringWithFormat:@"tel:%@",aPhoneNum];
        NSURL *phoneNumberURL = [NSURL URLWithString:str];
        [[UIApplication sharedApplication] openURL:phoneNumberURL];
    }else {
        UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:nil message:@"您的设备不支持打电话的功能"
                                                        delegate:nil cancelButtonTitle:@"确定"
                                               otherButtonTitles:nil];
        [_alert show];
        [_alert release];
    }
}

@end

@implementation UIView (Alert)
+ (void) showAlertTitle:(NSString*) aTitle
andContent:(NSString*) aContent
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:aTitle
                                                        message:aContent
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
    [alertView show];
    [alertView release];
    return;
}
@end